package blackjack;

import java.util.ArrayList;

public class Hand {
    private ArrayList<Card> cards;

    public Hand() {
        cards = new ArrayList<Card>();
    }

    // 将一张牌添加到手牌中
    public void addCard(Card card) {
        cards.add(card);
    }

    // 计算手牌的最优点数
    public int calculatePoints() {
        int res = 0;
        int aceCount = 0;
        for (Card card: cards) {
            if (card.getRank() == "Ace") {
                aceCount++;
            }
            else {
                res += card.getValue();
            }
        }
        for (int i = 0; i < aceCount; i++) {
            if (res + 11 + (aceCount - 1 - i) <= 21) {
                res += 11;
            }
            else {
                res += 1;
            }
        }
        return res;
    }

    // 检查是否爆牌
    public boolean isBusted() {
        return calculatePoints() > 21;
    }

    // 展示手牌
    public void displayHand() {
        for (Card card: cards) {
            card.displayCard();
        }
    }

    // 判断手牌是否形成 BlackJack
    public boolean isBlackJack() {
        if (cards.size() != 2) {
            return false;
        }
        if (calculatePoints() != 21) {
            return false;
        }
        return true;
    }

    public Card displayOne(){
        return cards.get(0);
    }
}
